{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "Lily\n",
      "Rose\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "print(name_list[0])  # Tom\n",
    "print(name_list[1])  # Lily\n",
    "print(name_list[2])  # Rose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "print(name_list.index('Lily', 0, 2))  # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "'Lilys' is not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-f8b75d13f9b3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mname_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Lilys'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: 'Lilys' is not in list"
     ]
    }
   ],
   "source": [
    "name_list.index('Lilys', 0, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "print(name_list.count('Lily'))  # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose', 'Lily']\n",
    "\n",
    "print(name_list.count('Lily'))  # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "# 结果：True\n",
    "print('Lily' in name_list)\n",
    "\n",
    "# 结果：False\n",
    "print('Lilys' in name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "# 结果：False\n",
    "print('Lily' not in name_list)\n",
    "\n",
    "# 结果：True\n",
    "print('Lilys' not in name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您要搜索的名字：Tom\n",
      "您输入的名字是Tom, 名字已经存在\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name = input('请输入您要搜索的名字：')\n",
    "\n",
    "if name in name_list:\n",
    "    print(f'您输入的名字是{name}, 名字已经存在')\n",
    "else:\n",
    "    print(f'您输入的名字是{name}, 名字不存在')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "功能：注册邮箱，用户输入账号名，判断这个账号名是否存在：\n",
    "    如果存在，提示用户\n",
    "    否则提示可以注册"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的邮箱账号名：Tomas\n",
      "Tomas√\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "name = input('请输入您的邮箱账号名：')\n",
    "if name in name_list:\n",
    "    print(f'您输入的用户名{name}已存在！')\n",
    "else:\n",
    "    print(f'{name}√')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily', 'Rose', 'xiaoming']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.append('xiaoming')\n",
    "\n",
    "# 结果：['Tom', 'Lily', 'Rose', 'xiaoming']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily', 'Rose', ['xiaoming', 'xiaohong']]\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.append(['xiaoming', 'xiaohong'])\n",
    "\n",
    "# 结果：['Tom', 'Lily', 'Rose', ['xiaoming', 'xiaohong']]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily', 'Rose', 'x', 'i', 'a', 'o', 'm', 'i', 'n', 'g']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.extend('xiaoming')\n",
    "\n",
    "# 结果：['Tom', 'Lily', 'Rose', 'x', 'i', 'a', 'o', 'm', 'i', 'n', 'g']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily', 'Rose', 'xiaoming', 'xiaohong']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.extend(['xiaoming', 'xiaohong'])\n",
    "\n",
    "# 结果：['Tom', 'Lily', 'Rose', 'xiaoming', 'xiaohong']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'xiaoming', 'Lily', 'Rose']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.insert(1, 'xiaoming')\n",
    "\n",
    "# 结果：['Tom', 'xiaoming', 'Lily', 'Rose']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'name_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-82891a72351d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m# 结果：报错提示：name 'name_list' is not defined\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mname_list\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'name_list' is not defined"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "# 结果：报错提示：name 'name_list' is not defined\n",
    "del name_list\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lily', 'Rose']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "del name_list[0]\n",
    "\n",
    "# 结果：['Lily', 'Rose']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lily\n",
      "['Tom', 'Rose']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "del_name = name_list.pop(1)\n",
    "\n",
    "# 结果：Lily\n",
    "print(del_name)\n",
    "\n",
    "# 结果：['Tom', 'Rose']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.remove('Rose')\n",
    "\n",
    "# 结果：['Tom', 'Lily']\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_list.clear()\n",
    "print(name_list) # 结果： []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8, 6, 3, 2, 5, 1]\n"
     ]
    }
   ],
   "source": [
    "num_list = [1, 5, 2, 3, 6, 8]\n",
    "\n",
    "num_list.reverse()\n",
    "\n",
    "# 结果：[8, 6, 3, 2, 5, 1]\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 5, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "num_list = [1, 5, 2, 3, 6, 8]\n",
    "\n",
    "num_list.sort()\n",
    "\n",
    "# 结果：[1, 2, 3, 5, 6, 8]\n",
    "print(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom', 'Lily', 'Rose']\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "name_li2 = name_list.copy()\n",
    "\n",
    "# 结果：['Tom', 'Lily', 'Rose']\n",
    "print(name_li2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "Lily\n",
      "Rose\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "i = 0\n",
    "while i < len(name_list):\n",
    "    print(name_list[i])\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom\n",
      "Lily\n",
      "Rose\n"
     ]
    }
   ],
   "source": [
    "name_list = ['Tom', 'Lily', 'Rose']\n",
    "\n",
    "for i in name_list:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [['小明', '小红', '小绿'], ['Tom', 'Lily', 'Rose'], ['张三', '李四', '王五']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', '李四', '王五']\n",
      "李四\n"
     ]
    }
   ],
   "source": [
    "# 第一步：按下标查找到李四所在的列表\n",
    "print(name_list[2])\n",
    "\n",
    "# 第二步：从李四所在的列表里面，再按下标找到数据李四\n",
    "print(name_list[2][1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 综合应用\n",
    "\n",
    "随机分配办公室\n",
    "\n",
    "步骤\n",
    "1. 准备数据\n",
    "    1. 9位老师 &rarr; 列表\n",
    "    2. 3个办公室 &rarr; 列表嵌套\n",
    "2. 分配老师到办公室\n",
    "3. 验证是否分配成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "teachers  = 'A B C D E F G H'.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "teachers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "office_list = [[], [], []]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分配老师到办公室\n",
    "\n",
    "import random\n",
    "\n",
    "for name in teachers:\n",
    "    office_list[random.randint(0,2)].append(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['D'], ['B', 'C'], ['A', 'E', 'F', 'G', 'H']]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "office_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "办公室1的人数是1, 老师分别是\n",
      "D\n",
      "办公室2的人数是2, 老师分别是\n",
      "B\n",
      "C\n",
      "办公室3的人数是5, 老师分别是\n",
      "A\n",
      "E\n",
      "F\n",
      "G\n",
      "H\n"
     ]
    }
   ],
   "source": [
    "# 验证是否分配成功\n",
    "i=1\n",
    "for office in office_list:\n",
    "    print(f'办公室{i}的人数是{len(office)}, 老师分别是')\n",
    "    for name in office:\n",
    "        print(name)\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
